﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ClipPlayer
{
    public class MediaFile
    {
        public string OriginalFile { get; set; }
        public string ResolvedFile { get; set; }
        //
        public override string ToString()
        {
            return "[" + OriginalFile + "] [" + ResolvedFile + "]";
        }
    }

    public class DirCache
    {
        public string DirectoryName { get; internal set; }
        public List<MediaFile> DirectoryFiles { get; internal set; }

        public DirCache()
        {
            DirectoryName = "";
            DirectoryFiles = new List<MediaFile>();
        }
    }


    public class DirectoryCache
    {
        public static Queue<DirCache> DC = new Queue<DirCache>(3);
        //
        //
        public static List<MediaFile> GetFilesFromDirectory(string fileOrDirectoryName)
        {
            //
            bool isDirectory = false;
            //
            //
            fileOrDirectoryName = System.IO.Path.GetFullPath(fileOrDirectoryName);
            string directoryName = "";
            //
            if (System.IO.Directory.Exists(fileOrDirectoryName))
            {
                directoryName = fileOrDirectoryName;
                isDirectory = true;
            }
            else
            {
                if (!System.IO.File.Exists(fileOrDirectoryName)) return new List<MediaFile>();
                directoryName = System.IO.Path.GetDirectoryName(fileOrDirectoryName);
                isDirectory = false;
            }
            //
            //
            //
            //
            foreach (DirCache dc in DC)
            {
                if (0 == string.Compare(dc.DirectoryName, directoryName, true))
                {
                    return getCuttedFileList(dc.DirectoryFiles, isDirectory, fileOrDirectoryName);
                }
            }
            //
            //List<string> ff = Directory.EnumerateFiles(directoryName, "*.*", SearchOption.TopDirectoryOnly).ToList<string>();
            //
            int filesCount = 0;
            foreach (string f in System.IO.Directory.EnumerateFiles(directoryName, "*.*", System.IO.SearchOption.TopDirectoryOnly))
            {
                filesCount++;
            }
            //
            List<MediaFile> df = new List<MediaFile>(filesCount + 100);// więcej na wszelki wypadek
            foreach (string file in System.IO.Directory.EnumerateFiles(directoryName, "*.*", System.IO.SearchOption.TopDirectoryOnly))
            {
                string resolvedName = ParseShortcut(file);
                if ("" == resolvedName) continue;
                if (!IsMediaFile(resolvedName)) continue;
                df.Add(new MediaFile() { OriginalFile = file, ResolvedFile = resolvedName });
            }
            //DC.Enqueue(new DirCache() { DirectoryName = directoryName, DirectoryFiles = new List<MediaFile>(df) });
            DC.Enqueue(new DirCache() { DirectoryName = directoryName, DirectoryFiles = df });
            //
            //
            return getCuttedFileList(DC.Last().DirectoryFiles, isDirectory, fileOrDirectoryName);
        }

        private static List<MediaFile> getCuttedFileList(List<MediaFile> df, bool isDirectory, string fileName)
        {
            if (!isDirectory)
            {
                int n = df.FindIndex(v => v.OriginalFile == fileName);
                if (n >= 0)
                {
                    MediaFile[] nDf = new MediaFile[df.Count - n];
                    df.CopyTo(n, nDf, 0, nDf.Count());
                    return nDf.ToList<MediaFile>();
                }
            }
            //
            return new List<MediaFile>(df);
        }
        public static bool IsMediaFile(string fileName)
        {
            string ext = System.IO.Path.GetExtension(fileName).ToUpper();
            //
            return ".AVI" == ext || ".MKV" == ext || ".MP4" == ext || ".VOB" == ext || ".FLV" == ext;
        }


        public static string ParseShortcut(string fname)
        {
            if (0 != string.Compare(".LNK", System.IO.Path.GetExtension(fname), true)) return fname;
            Utils.Shortcut link = new Utils.Shortcut();
            link.Load(fname);
            return link.Path;
        }

    }//class
}//namespace